<?php

$db_obj = new db_obj();
$client_obj = new client_obj();
$user_obj = new user_obj();
$filter_obj = new filter_obj();

if (isset($_REQUEST['action'])) {
    switch ($_REQUEST['action']) {
        case 'deleteClients':
            if (!empty($_REQUEST['clientId'])) {
                $success = true;
                foreach ($_REQUEST['clientId'] as $key => $id) {
                    $res = $client_obj->deleteClientById($id);
                    if (error_obj::isError($res)) {
                        error_obj::setError($res, UNEXCEPTED);
                        $success = false;
                    }
                }
                if ($success) {
                    error_obj::setGood("Clients were deleted!");
                    base_obj::redirect(ROOT . '?input=list_of_clients');
                }
            }
            break;
    }
}

if (!isset($_SESSION['list_of_clients_pager'])) {
    $_SESSION['list_of_clients_pager'] = $GLOBALS['list_of_clients_pager'];
}

if (!isset($_SESSION['list_of_clients_order'])) {
    $_SESSION['list_of_clients_order'] = $GLOBALS['list_of_clients_order'];
}

if (isset($_SESSION['list_of_clients_fulltext']) && !empty($_SESSION['list_of_clients_fulltext']))
    $GLOBALS['template_data']['currentFulltext'] = $_SESSION['list_of_clients_fulltext'];

$filter = array();

$currentFilter = array();

if (isset($_SESSION['list_of_clients_filter'])) {
    $filter = $_SESSION['list_of_clients_filter'];
    $result = $filter_obj->updateFilter($_SESSION['user']['user_id'], 'list_of_clients_filter', $filter);
    if (error_obj::isError($result)) {
        error_obj::setError($result, UNEXCEPTED);
    }
    foreach ($_SESSION['list_of_clients_filter'] as $f => $v) {
        switch ($f) {
            case 'city':
                $currentFilter[$f] = $v;
                break;
            case 'zip':
                $currentFilter[$f] = $v;
                break;
            case 'typeOfCooperation':
                $currentFilter[$f] = $client_obj->getClientTypeOfCooperation($v);
                $currentFilter[$f] = $currentFilter[$f]['type'];
                break;
            case 'contactPerson':
                $currentFilter[$f] = $user_obj->getUserById($v, array('name', 'surname'));
                $currentFilter[$f] = $currentFilter[$f][0] . ' ' . $currentFilter[$f][1];
                break;
            case 'active':
                if ($v == 0)
                    $currentFilter[$f] = 'Yes';
                else
                    $currentFilter[$f] = 'No';
                break;
            case 'bad_payer':
                if ($v == 1)
                    $currentFilter[$f] = 'Yes';
                else
                    $currentFilter[$f] = 'No';
                break;
        }
    }
}

$GLOBALS['template_data']['currentFilter'] = $currentFilter;

if (!isset($_GET['page'])) {
    $_GET['page'] = 0;
}

$limit = ($_GET['page'] * $_SESSION['list_of_clients_pager']) . ' ,' . $_SESSION['list_of_clients_pager'];

if (isset($_SESSION['list_of_clients_fulltext']) && !empty($_SESSION['list_of_clients_fulltext'])) {
    $PAGER_COUNT = $client_obj->getListOfClientsByFulltext($_SESSION['list_of_clients_fulltext'], $filter, $_SESSION['list_of_clients_order'], '', 'COUNT(*)');
    $clients = $client_obj->getListOfClientsByFulltext($_SESSION['list_of_clients_fulltext'], $filter, $_SESSION['list_of_clients_order'], $limit);
} else {
    $PAGER_COUNT = $client_obj->getListOfClients($filter, $_SESSION['list_of_clients_order'], '', 'COUNT(*)');
    $clients = $client_obj->getListOfClients($filter, $_SESSION['list_of_clients_order'], $limit);
}
$PAGER_COUNT = $PAGER_COUNT[0][0];

$GLOBALS['template_data']['currentOrder'] = array(
    $_SESSION['list_of_clients_order']['col'],
    $_SESSION['list_of_clients_order']['order']
);

$GLOBALS['template_data']['total_count'] = $PAGER_COUNT;

$pageSum = 0;
foreach ($clients as $key => $client) {
    if (!empty($client['contactPerson']))
        $clients[$key]['contact_person_info'] = $user_obj->getUserById($client['contactPerson'], array('name', 'surname'));
    if (!empty($client['typeOfCooperation'])) {
        $clients[$key]['cooperation_info'] = $client_obj->getClientTypeOfCooperation($client['typeOfCooperation']);
    } else {
        $clients[$key]['cooperation_info']['type'] = '-';
    }
    $pageSum += $client['monthlyFee'];
}

$GLOBALS['template_data']['pageSum'] = $pageSum;
$GLOBALS['template_data']['totalSum'] = $db_obj->getOne("SELECT SUM(monthlyFee) FROM " . $GLOBALS['tb_clients']);
$GLOBALS['template_data']['clients'] = $clients;

$pager = $db_obj->pager($PAGER_COUNT, $_GET['page'], $_SESSION['list_of_clients_pager']);

$GLOBALS['template_data']['pager'] = $pager;
?>
